<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cart;
use App\Order;
use App\Item;
class ShopController extends Controller
{
    private $gs = [
        1=>['goods_id'=>1,'goods_name'=>'白百合 清香宜人', 'price'=>23.9],
        2=>['goods_id'=>2,'goods_name'=>'红玫瑰 热烈奔放', 'price'=>222.2],
        3=>['goods_id'=>3,'goods_name'=>'黄牡丹 雍容华贵', 'price'=>12.3],
        4=>['goods_id'=>4,'goods_name'=>'狗尾巴 淡泊名利', 'price'=>32.4],
    ];

    public function goods($goods_id){
        $gs = $this->gs[$goods_id];
        return view('goods', ['gs'=>$gs]);
    }


    public function buy($goods_id){

        $g = $this->gs[$goods_id];
        Cart::add($g['goods_id'],$g['goods_name'],$g['price'],1);
        return redirect('cart');
    }

    public function cart(Request $req){
        if(!$req->session()->has('user')){ //判断用户是否通过微信认证
            return redirect('login');
        }

        $goods =  Cart::getContent();
        $total = Cart::getTotal();
        return view('cart',['goods'=>$goods,'total'=>$total]);

    }

    public function clear(Request $req){ //清空购物车
        if(!$req->session()->has('user')){ //判断用户是否通过微信认证
            return redirect('login');
        }

        Cart::clear();
        return redirect('/');
    }


    public function done(Request $req){ //订单入库
    /*
     *
     * oid
 orsn
 uid
 openid
 xm
 address
 money
 ispay
 ortime
     * */

        $user = $req->session()->get('user');
        dd($user);
        $order = new Order();
        $order->orsn = date('Ymd').mt_rand(1000000,9999999);
        $order->uid = $user->get();
    }



}
